﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;

namespace AutoChess
{
    public class ShipGridPanel : MonoBehaviour
    {
        public List<GameObject> shipGridRoot;

        public Text refreshCoin;
        public Text refreshCoinRed;
        public GroupSpriteKit lockKit;
        public GameObject RefreshBtn, LockBtn;
        public TweenerCtrl parentTweeCtr, tweeCtr;
        public GameObject shopLockObj, panelLockObj, panelUnlockObj;
        public Transform[] infoPos;
        public ACChessInfoWindow infoWindow;
        public GameObject redTipEff;

        private void Awake()
        {
            SetLockState();
        }
        public void OpenShop()
        {
            EventManager.AddEventListener(CommonEvent.On_Shop_Lock_State_Change, OnLockStateChange);
            EventManager.AddEventListener(CommonEvent.ON_AUTOCHESS_SWITCH_BATTLE, CloseShop);
            gameObject.SetActive(true);
            parentTweeCtr.PlayReverse();
            tweeCtr.PlayForward();

            var isSelfPlayer = AutoChessPlayerCtr.Ins.CurrentPlayer.IsSelfPlayer;
            RefreshBtn.SetActive(isSelfPlayer);//看别人不能刷新和锁住

            refreshCoin.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessRefreshCostGold);
            refreshCoinRed.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold < Table.DataModel.TableConfig.autoChessRefreshCostGold);
            var text = AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessRefreshCostGold ? refreshCoin : refreshCoinRed;
            text.text = "x " + Table.DataModel.TableConfig.autoChessRefreshCostGold;

            LockBtn.SetActive(isSelfPlayer);

            SetLockState();

            RefreshHeroItems();
            AutoChessFieldViewCtr.Ins.InputCtr.ResetPreviewControlData();
            AutoChessFieldViewCtr.Ins.SwitchCameraToUI = true;
        }
        public void RefreshHeroItems()
        {
            var shopHeros = AutoChessMapMgr.Ins.GetShopHeros();
            for (int i = 0; i < shopHeros.Count; i++)
            {
                var item = shipGridRoot[i].GetComponent<ShopHeroItem>();
                item.Refresh(shopHeros[i]);
            }
            infoWindow.gameObject.SetActive(false);
            FindSameChess();

            refreshCoin.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessRefreshCostGold);
            refreshCoinRed.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold < Table.DataModel.TableConfig.autoChessRefreshCostGold);
            var text = AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessRefreshCostGold ? refreshCoin : refreshCoinRed;
            text.text = "x " + Table.DataModel.TableConfig.autoChessRefreshCostGold;
        }
        private void SetLockState()
        {
            var islock = AutoChessManager.Ins.cacheData.CurPlayerData.LockState;
            lockKit.SetSpriteName(islock ? "zizouqi_icon_suo" : "zizouqi_icon_jiesuo");
            shopLockObj.SetActive(islock);
            panelLockObj.SetActive(islock);
            panelUnlockObj.SetActive(!islock);
        }
        public void CloseShop()
        {
            EventManager.RemoveEventListener(CommonEvent.On_Shop_Lock_State_Change, OnLockStateChange);
            EventManager.RemoveEventListener(CommonEvent.ON_AUTOCHESS_SWITCH_BATTLE, CloseShop);
            parentTweeCtr.PlayForward();
            tweeCtr.PlayReverse();

            AutoChessFieldViewCtr.Ins.SwitchCameraToUI = false;
            infoWindow.gameObject.SetActive(false);
        }
        private void OnLockStateChange()
        {
            SetLockState();
        }
        /// <summary>
        /// 刷新
        /// </summary>
        public void ClickRefreshShop()
        {
            if (AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= Table.DataModel.TableConfig.autoChessRefreshCostGold)
            {
                AutoChessManager.Ins.SendRefreshChess();
            }
            else
            {
                GlobalLazyMessageManager.Instance.CreateGlobalPopTip("coin_lack");
            }
        }
        /// <summary>
        /// 锁定商店刷新
        /// </summary>
        public void ClickLockShopHero()
        {
            AutoChessManager.Ins.SendCSLockShop();
        }

        public void OnClickChess(int index)
        {
            infoWindow.transform.SetParent(infoPos[index], false);
            for (int i = 0; i < AutoChessPlayerCtr.Ins.CurrentPlayer.HeroUnits.Count; i++)
            {
                var hero = AutoChessPlayerCtr.Ins.CurrentPlayer.HeroUnits[i];
                if(hero.MapGrid != null && hero.MapGrid.GridType == GridType.ShopGrid)
                {
                    if(hero.MapGrid.LocalIndex == index)
                    {
                        infoWindow.gameObject.SetActive(true);
                        infoWindow.SetData(hero.ChessID);
                        return;
                    }
                }
            }
        }

        private void FindSameChess()
        {
            redTipEff.SetActive(false);
            if (AutoChessPlayerCtr.Ins.CurrentPlayer.IsSelfPlayer)
            {
                Dictionary<int, int> notShop = new Dictionary<int, int>();
                Dictionary<int, int> shop = new Dictionary<int, int>();

                for (int i = 0; i < AutoChessPlayerCtr.Ins.CurrentPlayer.HeroUnits.Count; i++)
                {
                    var unit = AutoChessPlayerCtr.Ins.CurrentPlayer.HeroUnits[i];
                    if(unit.MapGrid.GridType == GridType.ShopGrid)
                    {
                        if (shop.ContainsKey(unit.ChessID))
                            ++shop[unit.ChessID];
                        else
                            shop[unit.ChessID] = 1;
                    }
                    else
                    {
                        if (notShop.ContainsKey(unit.ChessID))
                            ++notShop[unit.ChessID];
                        else
                            notShop[unit.ChessID] = 1;
                    }
                }
                foreach (var item in shop)
                {
                    if(notShop.ContainsKey(item.Key) && notShop[item.Key] == 2)
                    {
                        redTipEff.SetActive(true);
                        return;
                    }
                }
            }
        }
    }
}
